package com.foo.designpattern.interpreter;

/**
 * com.foo.designpattern.interpreter
 * Created with IntelliJ IDEA.
 * Created by jiang on 2016-06-12 14:38.
 *
 * @Description 一个Variable对象代表一个有名变量
 */

public class Variable implements Expression {
    private String name;

    public Variable(String name) {
        this.name = name;
    }

    @Override
    public boolean interpret(Context ctx) {
        return ctx.lookup(this);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj instanceof Variable) {
            return this.name.equals(((Variable) obj).name);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return this.toString().hashCode();
    }

    @Override
    public String toString() {
        return this.name;
    }
}
